﻿using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
using FluentAvalonia.UI.Controls;
using iOSClub.App.Desktop.Dialogs;
using iOSClub.App.Desktop.ViewModels.Pages;
using iOSClub.App.Desktop.Views;
using iOSClub.App.Share.WeatherModels;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;

namespace iOSClub.App.Desktop.Pages;

public partial class WeatherPage : UserControl
{
    public WeatherPage()
    {
        InitializeComponent();
    }

    protected override async void OnInitialized()
    {
        base.OnInitialized();
        
        await ((WeatherPageModel)DataContext!).Change(Weather.DefaultCity.name);
    }

    private async void SearchClick(object? sender, RoutedEventArgs e)
    {
        if (DataContext is not WeatherPageModel model) return;
        var td = new TaskDialog
        {
            Title = "WeatherCore",
            Header = "去看看其他城市",
            Content = new CityChoose(),
            IconSource = new SymbolIconSource() { Symbol = Symbol.WeatherCloudy },
            FooterVisibility = TaskDialogFooterVisibility.Never,
            Buttons =
            {
                TaskDialogButton.OKButton,
                TaskDialogButton.CloseButton
            },
            XamlRoot = (Visual)VisualRoot!
        };

        td.Closing += async (dialog, args) =>
        {
            if ((TaskDialogStandardResult)args.Result != TaskDialogStandardResult.OK) return;
            if (dialog.Content is not CityChoose choose) return;
            if (string.IsNullOrEmpty(choose.Box.Text)) return;
            await model.Change(choose.Box.Text);
        };


        await td.ShowAsync();
    }

    private void HourClick(object? sender, RoutedEventArgs e)
    {
        if (DataContext is not WeatherPageModel model) return;
        if (model.List.Hours == null!) return;
        var window = ViewOpera.GetView<MainWindow>(this);
        window?.OpenData([
            new LineSeries<double>
            {
                Values = model.List.Hours.Select(x => double.Parse(x.temp!)),
                Fill = null
            }
        ], "接下来的24小时");
    }
    
    private void DayClick(object? sender, RoutedEventArgs e)
    {
        if (DataContext is not WeatherPageModel model) return;
        if (model.List.Days == null!) return;
        var window = ViewOpera.GetView<MainWindow>(this);
        var a = new List<ISeries>()
        {
            new LineSeries<double>
            {
                Values = model.List.Days.Select(x => double.Parse(x.tempMax))
            },
            new LineSeries<double>()
            {
                Values = model.List.Days.Select(x => double.Parse(x.tempMin))
            }
        };
        window?.OpenData(a, "之后几天");
    }
    
    private async void LookClick(object? sender, RoutedEventArgs e)
    {
        if (sender is not Control control) return;
        var td = new TaskDialog
        {
            Title = "WeatherCore",
            FooterVisibility = TaskDialogFooterVisibility.Never,
            IconSource = new SymbolIconSource() { Symbol = Symbol.WeatherCloudy },
            Buttons =
            {
                TaskDialogButton.OKButton
            },
            XamlRoot = (Visual)VisualRoot!
        };

        if (control.DataContext is WeatherDay day)
        {
            // 2024-02-18
            var match = Regex.Match(day.fxDate, "(.*)-(.*)-(.*)");
            td.Header = match.Groups.Count < 4 ? day.fxDate : $"{match.Groups[2]}月{match.Groups[3]}日";
            td.Content = new DayWeatherLook() { DataContext = day };
            await td.ShowAsync();
            return;
        }

        if (control.DataContext is WeatherHour hour)
        {
            // 2024-02-18T00:00+08:00
            var match = Regex.Match(hour.fxTime!, "(.*)-(.*)-(.*)T(.*?):(.*)");
            td.Header =
                $"{match.Groups[1]}年{match.Groups[2]}月{int.Parse(match.Groups[3].Value)}日   {int.Parse(match.Groups[4].Value)}点";
            td.Content = new HourWeatherLook() { DataContext = hour };
        }

        await td.ShowAsync();
    }

    private async void SunClick(object? sender, RoutedEventArgs e)
    {
        if (DataContext is not WeatherPageModel model) return;
        var td = new TaskDialog
        {
            Title = "WeatherCore",
            Header = "日出而作 , 日落而息",
            IconSource = new SymbolIconSource() { Symbol = Symbol.WeatherCloudy },
            Content = new SunMoonLook() { DataContext = model.List.Today },
            FooterVisibility = TaskDialogFooterVisibility.Never,
            Buttons =
            {
                TaskDialogButton.OKButton
            },
            XamlRoot = (Visual)VisualRoot!
        };

        await td.ShowAsync();
    }

    private void SuggestionsClick(object? sender, RoutedEventArgs e)
    {
        SuggestionsList.IsVisible = !SuggestionsList.IsVisible;
    }

    private async void SuggestionTapped(object? sender, TappedEventArgs e)
    {
        if (sender is not Control control) return;
        if (control.DataContext is not WeatherSuggestion suggestion) return;
        var panel = new StackPanel();
        panel.Children.AddRange([
            new TextBlock()
            {
                TextWrapping = TextWrapping.WrapWithOverflow,
                Text = suggestion.category
            },
            new TextBlock()
            {
                TextWrapping = TextWrapping.WrapWithOverflow,
                Text = suggestion.text
            }
        ]);
        var td = new TaskDialog
        {
            Title = "WeatherCore",
            Header = suggestion.name,
            IconSource = new SymbolIconSource() { Symbol = Symbol.WeatherCloudy },
            Content = panel,
            FooterVisibility = TaskDialogFooterVisibility.Never,
            Buttons =
            {
                TaskDialogButton.OKButton
            },
            XamlRoot = (Visual)VisualRoot!
        };

        await td.ShowAsync();
    }
}